angular.module('app').controller('positionCtrl', ['$q', '$http', '$state', '$rootScope', '$scope', 'cache', function ($q, $http, $state, $rootScope, $scope, cache) {
    $scope.isActive = false;
    $scope.isLogin = cache.get('name');
    //console.log($rootScope);
    $scope.init(); //调用了rootScope的方法
    cache.put('name', 'xpl');//调用自定义服务cache
    cache.put('name2', 'xpl2');//调用自定义服务cache
    cache.remove('name2');//调用自定义服务cache
    function ajax_pos() {
        var def = $q.defer();
        $http({
            url: '/data/position.json',
            method: 'GET',
            headers: {
                'Content-Type': 'application/json', /*or whatever type is relevant */
                'Accept': 'application/json' /* ditto */
            },
            params: {
                id: $state.params.id
            }
        }).then(function (res) {
            $scope.positionInfo = res.data;
            if(res.data.iscollect == 1){
                $scope.isActive = true;
                $scope.$broadcast('getState',$scope.isActive);
            }
            def.resolve(res.data);
        }, function (error) {
            console.log(error);
            def.reject(error);
        });
        return def.promise;
    }
    function ajax_com(id) {
        var def = $q.defer();
        def.notify('loading......');
        $http({
            url: '/data/company.json',
            method: 'GET',
            params: {
                id: id
            }
        }).then(function (res) {
            //console.log(res.data);
            $scope.companyInfo = res.data;
            def.resolve(res.data);
        }, function (error) {
            console.log(error);
            def.reject(error);
        });
        return def.promise;
    }
    var getPos = ajax_pos();
    getPos.then(function(res) {
        var id = res.companyId;
        ajax_com(id);
    },function(erro) {
        console.log(erro);
    },function(res) {
        console.log(res);
    });

}]);
